﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using ERP_RORZE_DLL.Product.database;
using ERP_RORZE_DLL.Product.DAO;
using ERP_RORZE_DLL.Product.Base;
using ERP_RORZE_PERMISSION.Base;
using BaseDAO = ERP_RORZE_TOOL.DBAccess.BaseDAO;
using Utilities = ERP_RORZE_TOOL.Common.Utilities;

namespace ERP_RORZE_PRODUCT.Product.PartData
{
    public partial class Frm_OptionDataAdd : DevExpress.XtraEditors.XtraForm
    {
        ClsOptionDataTable obj = new ClsOptionDataTable();
        ClsPartDataDAO clspartDAO = new ClsPartDataDAO();
        DataTable dt;
        private LogUtils log;
        public EventHandler Re_Load;
        private string minDate = "1753-01-01 00:00:00.000";

        public Frm_OptionDataAdd()
        {
            InitializeComponent();
            log = new LogUtils();
        }

        void ThongBao(string name)
        {
            MessageBox.Show("Chưa nhập " + name + ". Vui lòng nhập lại");
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {

            #region
            //if (txtClampTime.Text == "")
            //{
            //    ThongBao(clsConstantsdb.Entities.ClsDBOptionData.FieldName.ClampTime);
            //}
            //else if (txtProTime.Text == "")
            //{
            //    ThongBao(clsConstantsdb.Entities.ClsDBOptionData.FieldName.ProTime);
            //}
            //else if (txtToolQty.Text == "")
            //{
            //    ThongBao(clsConstantsdb.Entities.ClsDBOptionData.FieldName.ToolQty);
            //}
            //else
            //{
            //    if (txtAondNote.Text == "")
            //    {
            //        txtAondNote.Text = "0";
            //    }
            //    try
            //    {

            //        float.Parse(txtAondNote.Text);
            //        float.Parse(txtClampTime.Text);
            //        float.Parse(txtProTime.Text);
            //        int.Parse(txtToolQty.Text);

            //        if (dateCLUpdate.EditValue == null)
            //        {
            //            //dateCLUpdateday.DateTime = DateTime.Parse("1753-01-01 00:00:00.000");
            //        }

            //        if (dateUpdate.EditValue == null)
            //        {
            //            //dateUpdateDay.DateTime = DateTime.Parse("1753-01-01 00:00:00.000");
            //        }
            //        if (cbStaffID.Text == "")
            //        {
            //            MessageBox.Show("Bạn phải nhập Staff");
            //        }
            //        else
            //        {
            //            //check dup partID and OptionID
            //            DataTable dtCheckDup =
            //                BaseDAO.ExecuteQuery("Select * from [242_OptionData] where [PartID] = '" + cbPartID.Text +
            //                                     "' and [OptionID] = '" + cbOptionID.Text + "'");
            //            if (dtCheckDup.Rows.Count > 1)
            //            {
            //                MessageBox.Show("Đã có PartNo và OptionID này trong dữ liệu.");
            //                return;
            //            }
            //        }


            //        obj.InputDate = dateInputDate.DateTime;
            //        obj.PartID = cbPartID.Text;
            //        obj.MachineID = cbMachineID.Text;
            //        obj.OptionID = cbOptionID.Text;
            //        obj.JigID = cbJigID.Text;
            //        obj.StaffID = cbStaffID.SelectedValue.ToString();
            //        obj.ToolQty = int.Parse(txtToolQty.Text);
            //        obj.ProTime = float.Parse(txtProTime.Text);
            //        obj.ClampTime = float.Parse(txtClampTime.Text);
            //        obj.TechDate = checkTechDate.Checked;
            //        obj.UpdateDay = dateUpdate.DateTime;
            //        obj.Note = txtNote.Text;
            //        obj.AondNote = float.Parse(txtAondNote.Text);
            //        obj.Memo = txtMemo.Text;
            //        obj.CLUpdateday = dateCLUpdate.DateTime;
            //        obj.LastOption = checkLastOption.Checked;
            //        obj.Jig = checkJig.Checked;
            //        obj.JigType = txtJigType.Text;


            //        //MessageBox.Show("" + dateUpdateDay.EditValue + ",,,,,," + dateCLUpdateday.EditValue);
            //        //MessageBox.Show(""+dateUpdateDay.DateTime+",,,,,,"+dateCLUpdateday.DateTime);

            //        clspartDAO.InsertOptionData(obj);
            //        MessageBox.Show("Đã nhập xong");

            //        cbPartID.SelectedItem = cbPartID.Items[0];
            //        cbMachineID.SelectedItem = cbMachineID.Items[0];
            //        cbOptionID.SelectedItem = cbOptionID.Items[0];
            //        cbJigID.SelectedItem = cbJigID.Items[0];
            //        cbStaffID.SelectedItem = cbStaffID.Items[0];

            //        dateCLUpdate.EditValue = null;
            //        dateUpdate.EditValue = null;
            //        txtJigType.Text = string.Empty;
            //        txtToolQty.Text = string.Empty;
            //        txtProTime.Text = string.Empty;
            //        txtClampTime.Text = string.Empty;
            //        txtAondNote.Text = string.Empty;
            //        txtNote.Text = string.Empty;
            //        txtMemo.Text = string.Empty;
            //        checkTechDate.Checked = false;
            //        checkLastOption.Checked = false;
            //        checkJig.Checked = false;

            //        //this.Re_Load(sender, e);
            //    }
            //    catch
            //    {
            //        MessageBox.Show("AondNote, ClampTime, ProTime, ToolQty phải là kiểu số");
            //    }

            //}
            #endregion

            AddData();
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void Frm_OptionDataAdd_Load(object sender, EventArgs e)
        {
            //lấy PartID  PartID

            dt = BaseDAO.ExecuteQuery("select [242_PartData].PartID from [242_PartData]");
            DataRow r1 = dt.NewRow();
            r1["PartID"] = "";
            dt.Rows.InsertAt(r1, 0);
            cbPartID.DataSource = dt;
            cbPartID.DisplayMember = "PartID";
            cbPartID.ValueMember = "PartID";

            // lấy StaffID
            DataTable dt1;
            dt1 = clspartDAO.GetAllStaffID();
            DataRow r = dt1.NewRow();
            r["FullName"] = "";
            r["StaffID"] = "";
            dt1.Rows.InsertAt(r, 0);
            cbStaffID.DataSource = dt1;
            cbStaffID.DisplayMember = "FullName";
            cbStaffID.ValueMember = "StaffID";

            // lấy OptionID
            DataTable dt2;
            dt2 = clspartDAO.GetAllOptionID();
            DataRow r2 = dt2.NewRow();
            r2["OptionID"] = "";
            r2["OptionID"] = "";
            dt2.Rows.InsertAt(r2, 0);
            cbOptionID.DataSource = dt2;
            cbOptionID.DisplayMember = "OptionID";
            cbOptionID.ValueMember = "OptionID";

            //
            DataTable dt3;
            dt3 = clspartDAO.GetAllMachineID();
            DataRow r3 = dt3.NewRow();
            r3["MachineID"] = "";
            r3["MachineID"] = "";
            dt3.Rows.InsertAt(r3, 0);
            cbMachineID.DataSource = dt3;
            cbMachineID.DisplayMember = "MachineID";
            cbMachineID.ValueMember = "MachineID";

            //Lấy jigID
            DataTable dt4;
            dt4 = clspartDAO.GetAllJigID();
            DataRow r4 = dt4.NewRow();
            r4["JigType"] = "";
            r4["JigName"] = "";
            dt4.Rows.InsertAt(r4, 0);
            cbJigType.DataSource = dt4;
            cbJigType.DisplayMember = "JigType";
            cbJigType.ValueMember = "JigType";

            dateInputDate.EditValue = DateTime.Now;
        }

        protected override bool ProcessKeyPreview(ref Message m)
        {
            if (m.Msg == 0x0100 && (int)m.WParam == 13)
            {
                this.ProcessTabKey(true);
            }
            return base.ProcessKeyPreview(ref m);
        }

        private void AddData()
        {
            ClsOptionDataTable obj = new ClsOptionDataTable();
            if (GetData(obj))
            {

                //clspartDAO.InsertOptionData(obj);
                if (clspartDAO.InsertOptionDataNew(obj) > 0)
                {

                    //write Log
                    log.Table = LogConstant.Table.Product.OptionData;
                    log.Task = LogConstant.Task.Insert;
                    log.Description = "OptionID: " + obj.OptionID + "; PartID: " + obj.PartID + "; InputDate: " + obj.InputDate;
                    log.WriteLog();

                    MessageBox.Show("Đã nhập xong");
                    //cbPartID.SelectedItem = cbPartID.Items[0];
                    //cbMachineID.SelectedItem = cbMachineID.Items[0];
                    //cbOptionID.SelectedItem = cbOptionID.Items[0];
                    cbJigType.SelectedItem = cbJigType.Items[0];
                    cbStaffID.SelectedItem = cbStaffID.Items[0];

                    dateCLUpdate.EditValue = null;
                    dateUpdate.EditValue = null;
                    txtJigID.Text = string.Empty;
                    txtToolQty.Text = string.Empty;
                    txtProTime.Text = string.Empty;
                    txtClampTime.Text = string.Empty;
                    txtAondNote.Text = string.Empty;
                    txtNote.Text = string.Empty;
                    txtMemo.Text = string.Empty;
                    checkTechDate.Checked = false;
                    checkLastOption.Checked = false;
                    checkJig.Checked = false;
                    cbOptionID.Focus();

                }
                else
                {
                    Utilities.MessageErrorOk("An error occurred while inserting the record to database. Please try again.");
                }
            }
        }

        private bool GetData(ClsOptionDataTable obj)
        {
            #region Check valid

            if (Utilities.IsNullOrEmpty(dateInputDate.EditValue.ToString()))
            {
                MessageBox.Show("Bạn phải nhập Input Date.");
                dateInputDate.Focus();
                return false;
            }

            if (Utilities.IsNullOrEmpty(cbPartID.SelectedValue) || cbPartID.SelectedItem == cbPartID.Items[0])
            {
                MessageBox.Show("Bạn phải nhập PartID.");
                cbPartID.Focus();
                return false;
            }

            if (Utilities.IsNullOrEmpty(cbMachineID.SelectedValue) || cbMachineID.SelectedItem == cbMachineID.Items[0])
            {
                MessageBox.Show("Bạn phải nhập MachineID.");
                cbMachineID.Focus();
                return false;
            }

            if (Utilities.IsNullOrEmpty(cbOptionID.SelectedValue) || cbOptionID.SelectedItem == cbOptionID.Items[0])
            {
                MessageBox.Show("Bạn phải nhập OptionID.");
                cbOptionID.Focus();
                return false;
            }

            //check dup partID and OptionID
            DataTable dtCheckDup =
                BaseDAO.ExecuteQuery("Select * from [242_OptionData] where [PartID] = N'" + cbPartID.Text +
                                        "' and [OptionID] = N'" + cbOptionID.Text + "'");
            if (dtCheckDup.Rows.Count > 0)
            {
                MessageBox.Show("Đã có PartNo và OptionID này trong dữ liệu.");
                cbPartID.Focus();
                return false;
            }


            //if (Utilities.IsNullOrEmpty(cbJigID.SelectedValue) || cbJigID.SelectedItem == cbJigID.Items[0])
            //{
            //    MessageBox.Show("Bạn phải nhập Jig.");
            //    cbJigID.Focus();
            //    return false;
            //}

            //if (Utilities.IsNullOrEmpty(cbStaffID.SelectedValue) || cbStaffID.SelectedItem == cbStaffID.Items[0])
            //{
            //    MessageBox.Show("Bạn phải nhập StaffID.");
            //    cbStaffID.Focus();
            //    return false;
            //}

            if (!txtJigID.Text.Trim().Equals(string.Empty) && Utilities.IsNullOrEmpty(cbJigType.Text.Trim()))
            {
                MessageBox.Show("Bạn phải chọn Jig Type.");
                cbJigType.Focus();
                return false;
            }

            if (Utilities.IsNullOrEmpty(txtToolQty.Text.Trim()))
            {
                //MessageBox.Show("Bạn phải nhập Tool Qty.");
                txtToolQty.Text = "0";
                txtToolQty.Focus();
                //return false;
            }

            if (Utilities.IsNullOrEmpty(txtProTime.Text.Trim()))
            {
                MessageBox.Show("Bạn phải nhập ProTime.");
                txtProTime.Focus();
                return false;
            }


            if (Utilities.IsNullOrEmpty(txtClampTime.Text.Trim()))
            {
                MessageBox.Show("Bạn phải nhập ClampTime.");
                txtClampTime.Focus();
                return false;
            }

            if (Utilities.IsNullOrEmpty(txtAondNote.Text.Trim()))
            {
                txtAondNote.Text = "0";
                txtAondNote.Focus();
                //return false;
            }

            try
            {
                float.Parse(txtProTime.Text);
                float.Parse(txtClampTime.Text);
                int.Parse(txtToolQty.Text);
            }
            catch (Exception)
            {
                MessageBox.Show("ToolQty, ProTime, ClampTime phải là số.");
                txtProTime.Focus();
                return false;
            }

            if (float.Parse(txtToolQty.Text) < 0 || float.Parse(txtProTime.Text) < 0 || float.Parse(txtClampTime.Text) < 0)
            {
                MessageBox.Show("ToolQty, ProTime, ClampTime phải lớn hơn hoặc bằng 0.");
                txtProTime.Focus();
                return false;

            }

            #endregion

            obj.InputDate = dateInputDate.DateTime;
            obj.PartID = cbPartID.Text;
            obj.MachineID = cbMachineID.Text;
            obj.OptionID = cbOptionID.Text;
            obj.JigID = txtJigID.Text;
            obj.StaffID = cbStaffID.SelectedValue.ToString();
            obj.ToolQty = int.Parse(txtToolQty.Text);
            obj.ProTime = float.Parse(txtProTime.Text);
            obj.ClampTime = float.Parse(txtClampTime.Text);
            obj.TechDate = checkTechDate.Checked;

            obj.UpdateDay = (dateUpdate.EditValue == null) ? DateTime.Parse(minDate) : dateUpdate.DateTime;
            obj.CLUpdateday = (dateCLUpdate.EditValue == null) ? DateTime.Parse(minDate) : dateCLUpdate.DateTime;

            obj.Note = txtNote.Text;
            obj.AondNote = txtAondNote.Text;
            obj.Memo = txtMemo.Text;
            obj.LastOption = checkLastOption.Checked;
            obj.Jig = checkJig.Checked;
            obj.JigType = cbJigType.Text;

            return true;
        }

        private void Frm_OptionDataAdd_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Alt)
            {
                if (e.KeyCode == Keys.D)
                {
                    this.btnAdd.PerformClick();
                }
            }
            else if (e.KeyCode == Keys.End)
            {
                btnAdd.Focus();
            }
            else if (e.KeyCode == Keys.Home)
            {
                cbPartID.Focus();
            }
        }


    }
}